package com.hlf.modules.api.controller;

/**
 * Created by Administrator on 2017/9/5.
 *
 * 商户信息
 *
 */

import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.annotation.LoginUser;
import com.hlf.modules.merchant.merchant.service.MerchantService;
import com.hlf.modules.merchant.store.entity.Store;
import com.hlf.modules.merchant.store.service.StoreService;
import com.hlf.modules.sys.controller.AbstractController;
import com.hlf.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商家信息管理接口
 */
@RestController
@RequestMapping("/api/merchant")
@Api("商家信息管理接口")
public class ApiMerchantController extends AbstractController{

    @Autowired
    private MerchantService merchantService;
    @Autowired
    private StoreService storeService;

    /**
     * 门店列表
     */
    @GetMapping("storeList")
    @ApiOperation(value = "门店列表", notes = "门店列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType="string", name = "token", value = "token", required = true)
    })
    public ReturnResult storeList(@LoginUser SysUserEntity user) {
        Long userId = user.getUserId();
        Long merchantId = merchantService.queryForUserId(userId).getId();
        List<Store> storeList = storeService.queryListForMerchantId(merchantId);
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,storeList);
    }

    /**
     * 切换店铺
     */
    @SysLog("切换店铺")
    @PostMapping("changeStore")
    @ApiOperation(value = "切换店铺", notes = "切换店铺")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType="string", name = "newStoreId", value = "新门店id", required = true),
            @ApiImplicitParam(paramType = "query", dataType="string", name = "token", value = "token", required = true)
    })
    public ReturnResult changeStore(String newStoreId,@LoginUser SysUserEntity user) {
        Assert.isBlank(newStoreId, "新门店id不能为空");
        logger.info("新店铺id{}"+newStoreId);
        Long userId = user.getUserId();
        Long merchantId = merchantService.queryForUserId(userId).getId();

        merchantService.changeStore(merchantId,newStoreId);
        return ReturnResult.ok();
    }



}
